package player

import (
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/sp"
)

func (p *Player) MagicFurnaceRequest(req *sp.MagicFurnaceRequest) (res *sp.MagicFurnaceResponse, err error) {
	mData := make(map[int64]int64, 0)
	for _, item := range req.Goods {
		mData[item.Tid] = item.Num
	}
	uProp, resGoods, err := lib.MagicFurnace("MagicFurnaceRequest", p.User, mData, p.Send)
	if err != nil {
		res = &sp.MagicFurnaceResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	// 有成就完成，进行通知
	achLst := lib.AchievComplete(p.User)
	if len(achLst) > 0 {
		p.Send(&sp.AchievementNoticeRequest{
			Lst: achLst,
		})
	}
	res = &sp.MagicFurnaceResponse{
		Reward: lib.FormatGoodsInfoSp(resGoods),
	}

	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})

	return
}
